#!/bin/bash
#实现批量服务器scp传输文件到远端家目录
#只需更新SCPFILE文件路径及主机信息

#要传送的文件
SCPFILE=~/check.sh

#主机信息
echo "
#IPADD              USER                PWD
192.168.101.240      root                123456789
192.168.101.239      root                123456789

" >/tmp/host
########################################################
grep -Ev "^#|^$" /tmp/host >/tmp/host.txt
SUM=`cat /tmp/host.txt |wc -l`
for i in `seq $SUM`
do

IPADD=`sed -n "$i"p /tmp/host.txt|awk '{print $1}'`
USER=`sed -n "$i"p /tmp/host.txt |awk '{print $2}'`
PWD=`sed -n "$i"p /tmp/host.txt  |awk '{print $3}'`


#scp传送巡检脚本
expect <<EOF
         set timeout -1;
         spawn  scp $SCPFILE  $USER@$IPADD:
         expect {
                 "yes/no" { send "yes\n" ; exp_continue }
                 "password:" { send "$PWD\n" }
         }
         expect eof
EOF
echo "=========$IPADD SCP完成 ================="
done
rm -f /tmp/host*